import { store } from '@/stores'

export const mixin = {

  methods: {
    tableRowClass(val) {
      if (val.row.$delAnim) {
        return 'delAnim'
      } else {
        return ''
      }
    },
    calIndex(index) {
      // 当前页数 - 1 * 每页数据条数 + 1
      const page = this.listQuery.page // 当前页码
      const pagesize = this.listQuery.limit // 每页条数
      return index + 1 + (page - 1) * pagesize
    },
    clearQuery() {
      Object.keys(this.listQuery).forEach(property => {
        if (property === 'page') {
          this.listQuery.page = 1
        } else if (property === 'limit') {
          this.listQuery.limit = store.dictStore.defaultPageSize
        } else {
          this.listQuery[property] = undefined
        }
      })
    }
  }
}

export const treeMixin = {
  methods: {
    /**
     * 刷新树节点
     * @param list 刷新的节点数据
     * @param row 当前节点
     * @param result 查找结果，其中node是当前树表中发生变化对的那个点，list是所在的数组，parentNode是他更新后的父节点
     */
    searchTree(list, row, result) {
      if (result.node !== undefined && result.parentNode !== undefined) {
        return
      }
      list.forEach(item => {
        if (item.id === row.id) {
          result.node = item
          result.list = list
        }
        if (item.id === row.parentId) {
          result.parentNode = item
        }
        if (!this.isNull(item.children)) {
          this.searchTree(item.children, row, result)
        }
      })
    },
    /**
     * 刷新树节点
     * @param list 刷新的节点数据
     * @param row 当前节点
     */
    refreshTree(list, row, result) {
      // 新增节点
      if (result.node === undefined) {
        // 一级列表新增一个节点
        if (row.parentId === undefined) {
          list.push(row)
        } else {
          if (result.parentNode.children === undefined) {
            result.parentNode.children = []
          }
          result.parentNode.children.push(row)
        }
      } else {
        // 更新节点
        // 节点位置没变化
        row.children = result.node.children
        if (row.parentId === result.node.parentId) {
          Object.assign(result.node, row)
        } else {
          // 从原节点删除，再添加到新的节点中
          result.list.splice(result.list.indexOf(result.node), 1)
          if (row.parentId === undefined) {
            list.push(row)
          } else {
            if (result.parentNode.children === undefined) {
              result.parentNode.children = []
            }
            result.parentNode.children.push(row)
          }
        }
      }
    },
    /**
     * 删除树节点
     * @param row 删除的节点数据
     */
    deleteTree(list, row) {
      list.forEach((item) => {
        if (item.id === row.id) {
          this.deleteSubTreeAnim(row)
          setTimeout(() => {
            list.splice(list.indexOf(row), 1)
          }, 500)
        } else if (!this.isNull(item.children)) {
          this.deleteTree(item.children, row)
        }
      })
    },
    /**
     * 给所有子节点加上删除动画
     * @param row 删除的节点数据
     */
    deleteSubTreeAnim(row) {
      row.$delAnim = true
      if (this.isNull(row.children)) {
        return
      }
      row.children.forEach(item => {
        item.$delAnim = true
        this.deleteSubTreeAnim(item)
      })
    }
  }
}
